home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11751 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.ichange.com!newsmaster
  2. From: Jesse Liberty <jl@staff.ichange.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie Question on using function ptr or Casting?
  5. Date: Fri, 15 Mar 1996 14:07:59 -0500
  6. Organization: AT&T
  7. Message-ID: <3149C00F.1CFA@staff.ichange.com>
  8. References: <19960314170244.sstryker@sover.net>
  9. NNTP-Posting-Host: 140.244.99.60
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14. CC: jl@staff.ichange.com
  15.  
  16. Stew Stryker wrote:
  17. >... He has a base class which implements a linked list.  One of its methods is
  18. > called get_next_record(), which returns the pointer to the next record...
  19. > In the derived class, which holds a list of employees, he's printing out
  20. > all the values in the list in a print() function 
  21.  
  22. > void employees::print() {
  23. >     employees *current = this;
  24. >     while (current) {
  25. >         cout "Employee name: " << current->emp_name << "\n";
  26. >                 current = (employees*)current->get_next_record();
  27. >         }
  28. >     cout << "End of employee list.\n";
  29. > }
  30. > My confusion is on the line that reads:
  31. >         current = (employees*)current->get_next_record();
  32. > He says that he's casting the results of the linked_list get_next_record()
  33. > function from a void pointer (which is how this base class method is
  34. > defined) to a pointer of the employees type.  In my reading of my only C++
  35. > manual (Teach Yourself C++ Programming in 21 Days, by Jesse Liberty), this
  36. > looks kinda like a function pointer.  This book also refers briefly to
  37. > casting of data, and says it's generally a sign of a poor design.
  38. > ...       void*   get_next_record();
  39.  
  40. Nope, not a function. The syntax 
  41.  
  42.         current = (employees*)current->get_next_record();
  43.  
  44. says to the compiler:
  45.  
  46.     "take current and cast it to be an employees pointer and then 
  47. use that pointer to call get_next_record() assigning the result to 
  48. current."
  49.  
  50. Your buddy's code has get_next_record returning a void * which can point 
  51. to anything but which must be cast to point to a particular type before 
  52. it can be used.  This is a c-programmer's solution to this type of list 
  53. problem, but C++ offers a far better (type safe) alternative: templates. 
  54.  
  55.  
  56. By the way, while it is true that I said that casting is often a sign of 
  57. poor design, I was referring to casting down a pointer to a base object 
  58. to its "real" derived type -- a different issue than we have here.  
  59.  
  60.  
  61.  
  62. -- 
  63. ------
  64. Jesse Liberty [AT&T New Media Services]
  65. jl@staff.ichange.com    ZDNet: 72241,72
  66. Teach Yourself C++ In 21 Days. Sams 1994
  67. Teach Yourself MORE C++ In 21 Days. Sams 1996
  68. Teach Yourself ANSI C++ In 21 Days. Sams 1996
  69. C++: An Introduction To Programming. Que 1996
  70.